﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace badminton.Controls
{
    public partial class HallGrid : System.Web.UI.UserControl
    {
        protected int bindIdx = 1;
        protected int bindIdx2 = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            {
                BadmintonEntities bde = new BadmintonEntities();
                Repeater1.DataSource = bde.CityArea;
                Repeater1.DataBind(); 
                 
            }

        }

        protected void OnItemDataBind(object sender, RepeaterItemEventArgs e)
        {
            BadmintonEntities bde = new BadmintonEntities();
            if (e.Item.ItemType == ListItemType.Footer)
            {
                // fill the last td if the city area number is odd
                if (bde.CityArea.Count() % 2 != 0)
                {
                    Literal lit = e.Item.FindControl("tdPlaceHolder") as Literal;
                    lit.Text = "<td></td>";
                }
            } 
            
            if (e.Item.ItemType != ListItemType.Item && 
                e.Item.ItemType !=  ListItemType.AlternatingItem )
            {
                return;
            }
            if (bindIdx % 2 == 0 && bindIdx!=bde.CityArea.Count())
            {
                e.Item.Controls.Add(new LiteralControl("</tr><tr class='thinframetable'>"));
            }
            bindIdx++;

            bindIdx2 = 1;

            /////////////
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater repeater2 = (Repeater)e.Item.FindControl("Repeater2");
                CityArea ca = (CityArea)e.Item.DataItem;

                repeater2.DataSource = ca.Hall;
                repeater2.DataBind();
            }

        }


        protected void OnItemDataBind2(object sender, RepeaterItemEventArgs e)
        {

            if (e.Item.ItemType != ListItemType.Item &&
                e.Item.ItemType != ListItemType.AlternatingItem)
            {
                return;
            }
            BadmintonEntities bde = new BadmintonEntities();
            Repeater repeater2 = sender as Repeater;
            System.Data.Objects.DataClasses.EntityCollection<Hall> ds = repeater2.DataSource as System.Data.Objects.DataClasses.EntityCollection<Hall>;
            int num = ds.Count();
            if (bindIdx2 % 2 == 0 && bindIdx2!=num)
            {
                e.Item.Controls.Add(new LiteralControl("</tr><tr>"));
            }
            bindIdx2++;
        }

    }
}